<template>
    <div class="quteam-channel-list" v-loading="loading">
        <div class="input-box">
            <el-date-picker
                v-model="date"
                clearable
                type="daterange"
                align="right"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions"
                @change="dateChange">
            </el-date-picker>
            <el-input
                placeholder="请输入渠道码或渠道名称"
                @keyup.enter.native="getList(1)"
                style="width:200px;margin-left:20px;"
                clearable
                prefix-icon="el-icon-search"
                @clear="getList(1)"
                v-model.trim="paramsList.channel">
            </el-input>
        </div>

        <div class="table-box">
            <el-table :data="tableData" ref="table" border style="width: 100%">
                <el-table-column min-width="120" label="渠道名称" prop="desc"></el-table-column>
                <el-table-column min-width="100" label="渠道码" prop="channel"></el-table-column>
                <el-table-column min-width="90" label="今日新增" prop="todayAdd">
                    <template slot-scope="scope">
                        <div v-if="scope.row.todayAdd && scope.row.todayAdd >= 0">{{ scope.row.todayAdd }}</div>
                        <div v-else>0</div>
                    </template>
                </el-table-column>
                <el-table-column min-width="90" label="昨日新增" prop="yesAdd">
                    <template slot-scope="scope">
                        <div v-if="scope.row.yesAdd && scope.row.yesAdd >= 0">{{ scope.row.yesAdd }}</div>
                        <div v-else>0</div>
                    </template>
                </el-table-column>
                <el-table-column min-width="90" label="累计新增" prop="totalAdd">
                    <template slot-scope="scope">
                        <div v-if="scope.row.totalAdd && scope.row.totalAdd >= 0">{{ scope.row.totalAdd }}</div>
                        <div v-else>0</div>
                    </template>
                </el-table-column>
                <el-table-column min-width="130" label="近30日平均新增" prop="monthAverageAdd">
                    <template slot-scope="scope">
                        <div v-if="scope.row.monthAverageAdd && scope.row.monthAverageAdd >= 0">{{ scope.row.monthAverageAdd }}</div>
                        <div v-else>0</div>
                    </template>
                </el-table-column>
                <el-table-column min-width="90" label="昨日活跃" prop="yesActive">
                    <template slot-scope="scope">
                        <div v-if="scope.row.yesActive && scope.row.yesActive >= 0">{{ scope.row.yesActive }}</div>
                        <div v-else>0</div>
                    </template>
                </el-table-column>
                <el-table-column min-width="90" label="今日活跃" prop="todayActive">
                    <template slot-scope="scope">
                        <div v-if="scope.row.todayActive && scope.row.todayActive >= 0">{{ scope.row.todayActive }}</div>
                        <div v-else>0</div>
                    </template>
                </el-table-column>
                <el-table-column min-width="120" label="近30日活跃" prop="monthAverageActive">
                    <template slot-scope="scope">
                        <div v-if="scope.row.monthAverageActive && scope.row.monthAverageActive >= 0">{{ scope.row.monthAverageActive }}</div>
                        <div v-else>0</div>
                    </template>
                </el-table-column>
                <el-table-column min-width="170" label="上线日期">
                    <template slot-scope="scope">
                        <div v-if="scope.row.upTime">{{ tableFormatDate(scope.row.upTime) }}</div>
                        <div v-else>-</div>
                    </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <div>
                            <el-button @click="editTag(scope.row)" type="text" size="small" style="color:#67C23A;">编辑</el-button>
                        </div>
                        <div>
                            <el-button @click="goChannelDetail(scope.row.channel)" type="text" size="small">查看详情</el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>

        <el-dialog
            title="编辑渠道"
            :visible.sync="showEdit"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :show-close="false"
            class="tag-editor-box"
            width="800px">
            <el-form
                :model="currentItem"
                :rules="rules"
                ref="ruleForm"
                hide-required-asterisk
                label-width="70px">
                <el-form-item label="渠道" prop="desc">
                    <el-input v-model.trim="currentItem.desc"></el-input>
                </el-form-item>
                <el-form-item label="渠道码" prop="channel">
                    <el-input disabled v-model.trim="currentItem.channel"></el-input>
                </el-form-item>
            </el-form>

            <span slot="footer">
                <el-button @click="cancleSave">取 消</el-button>
                <el-button  type="primary" @click="saveTag">保 存</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { formatDate } from '@/utils/utils'

export default {
    data () {
        return {
            pageNum: 1,
            pageSize: this._pageSize,
            total: 0,
            loading: false,
            paramsList: {},
            date: [new Date('2020-09-17').getTime(), new Date().getTime()],
            tableData: [],
            pickerOptions: {
                disabledDate (time) {
                    return time.getTime() < new Date('2020-08-07 00:00:00').getTime()
                },
                shortcuts: [
                    {
                        text: '最近7天',
                        onClick (picker) {
                            const end = new Date()
                            const start = new Date()
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
                            end.setTime(end.getTime() - 3600 * 1000 * 24)
                            picker.$emit('pick', [start, end])
                        }
                    },
                    {
                        text: '最近30天',
                        onClick (picker) {
                            const end = new Date()
                            const start = new Date()
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
                            end.setTime(end.getTime() - 3600 * 1000 * 24)
                            picker.$emit('pick', [start, end])
                        }
                    }
                ]
            },
            rules: {
                desc: [
                    { required: true, message: '请输入渠道名称', trigger: 'blur' }
                ],
                channel: [
                    { required: true, message: '请输入渠道码', trigger: 'blur' }
                ]

            },
            showEdit: false, // 是否显示编辑、创建标签弹窗
            currentItem: {} // 被编辑对象
        }
    },
    created () {
        this.paramsList.fromDate = formatDate(new Date('2020-09-17'), 'YYYY-MM-DD') + ' 00:00:00'
        this.paramsList.endDate = formatDate(new Date(), 'YYYY-MM-DD') + ' 23:59:59'
        this.getList(1)
    },
    methods: {
        // 获取列表
        async getList (pageNum) {
            if (this.loading) return
            this.pageNum = pageNum
            this.loading = true
            let params = {
                pageNum,
                pageSize: this.pageSize
            }
            params = Object.assign(params, this.paramsList)
            let res = await this.$http.post('/team/selectChannelData', params)
            if (res.code === '0') {
                this.tableData = res.data
                this.total = res.count
            } else {
                this.$message({
                    type: 'error',
                    message: '系统异常，请稍后再试'
                })
            }
            this.loading = false
        },

        // 页码切换
        sizeChange (val) {
            this.pageNum = 1
            this.pageSize = val
            this.getList(1)
        },

        // 页容切换
        pageChange (val) {
            this.pageNum = val
            this.getList(this.pageNum)
        },

        // 查看用户详情
        goChannelDetail (channel) {
            const { href } = this.$router.resolve({
                path: '/team/teamChannel/channelDetail',
                query: {
                    channel
                }
            })
            window.open(href, '_blank')
        },
        dateChange (val) {
            if (val) {
                this.paramsList.fromDate = formatDate(new Date(val[0]), 'YYYY-MM-DD') + ' 00:00:00'
                this.paramsList.endDate = formatDate(new Date(val[1]), 'YYYY-MM-DD') + ' 23:59:59'
            } else {
                this.paramsList.fromDate = formatDate(new Date('2020-09-17'), 'YYYY-MM-DD') + ' 00:00:00'
                this.paramsList.endDate = formatDate(new Date(), 'YYYY-MM-DD') + ' 23:59:59'
            }
            this.getList(1)
        },
        tableFormatDate (time) {
            return formatDate(time, 'YYYY-MM-DD HH:MM:SS')
        },

        // 编辑标签
        editTag (item) {
            this.currentItem = { ...item }
            this.showEdit = true
        },

        // 取消保存
        cancleSave () {
            this.showEdit = false
            this.$refs.ruleForm.clearValidate()
        },

        // 保存标签
        saveTag () {
            this.$refs.ruleForm.validate(async (valid) => {
                if (valid) {
                    let res = await this.$http.post('/apkManage/saveChannel', this.currentItem)
                    if (res.code === '0') {
                        this.$message({
                            type: 'success',
                            message: '新建成功'
                        })
                        this.showEdit = false
                        this.getList(this.pageNum)
                    } else {
                        this.$message({
                            type: 'error',
                            message: res.desc
                        })
                    }
                } else {
                    return false
                }
            })
        }
    }
}
</script>

<style lang="scss" scoped>
    .quteam-channel-list {
        .input-box {
            margin-top: 10px;
            font-size: 13px;
            font-weight: 500;
            .input-title {
                margin: 0 10px 0 20px;
            }
            .btn {
                margin-top: 10px;
                margin-left: 10px;
            }
        }

        .table-box {
            margin: 20px 0;
            .cover {
                width: 80px;
                height: 80px;
                > img {
                    width: 100%;
                    height: 100%;
                }
            }
            .user-icon {
                width: 20px;
                height: 20px;
                border-radius: 50%;
                margin-right: 5px;
                vertical-align: top;
            }
            .table-btn {
                cursor: pointer;
                color: #409EFF;
                font-size: 13px;
                font-weight: 500;
                &:hover {
                    text-decoration: underline;
                }
            }
        }

        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
    }
</style>
